<template>
  <div class="app-container">
    <el-card>
      <!-- 搜索 -->
      <el-form :inline="true" :model="listQuery" size="small">
        
        <el-form-item>
          <el-select
            size="small"
            v-model="listQuery.retailer_id"
            clearable
            placeholder="请选择商家"
          >
            <el-option
              v-for="item in retailers"
              :key="item.value"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleFilter"
          >
            查询
          </el-button>
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="handleBatchDelete"
          >
            批量删除
          </el-button>
        </el-form-item>
      </el-form>
      <!-- 列表 -->
      <el-table
        style="width: 100%"
        v-loading="listLoading"
        border
        fit
        stripe
        highlight-current-row
        size="small"
        :data="datas"
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="#" align="center" type="selection">
        </el-table-column>
        <el-table-column
          v-for="column in tableColumns"
          :label="column.title"
          :prop="column.key"
          :key="column.key"
          :align="column.align"
          :width="column.width ? column.width : ''"
        >
        </el-table-column>
        <el-table-column align="center" label="操作" width="120px">
          <template slot-scope="scope">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="handleDelete(scope)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.page_size"
        @pagination="getBalanceBills"
      />
      <!-- 分页 end -->
    </el-card>

    <!-- 对话框 -->
    <el-dialog :title="getTitle" :visible.sync="dialogVisible" width="750px">
      <el-form
        :model="form"
        ref="formRef"
        label-width="100px"
        label-position="right"
        style="padding: 0 30px"
        size="small"
      >
      <el-row :gutter="10">
          <el-col :span="24">
            <el-form-item label="品鉴卡" prop="card_name">
              <el-input
                v-model="form.card_name"
                placeholder="品鉴卡"
                disabled
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input
                v-model="form.name"
                placeholder="姓名"
                disabled
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="微信ID" prop="openid">
              <el-input v-model="form.openid" placeholder="微信ID" clearable disabled>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="24">
            <el-form-item label="商家名称" prop="retailer">
              <el-input
                v-model="form.retailer_name"
                placeholder="商家名称"
                disabled
              >
              </el-input>
            </el-form-item>
          </el-col>
          
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="员工姓名" prop="r_name">
              <el-input v-model="form.r_name" placeholder="员工姓名" disabled>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="员工微信ID" prop="r_openid">
              <el-input
                v-model="form.r_openid"
                placeholder="员工微信ID"
                disabled
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="核销时间" prop="update_time">
              <el-input v-model="form.update_time" placeholder="核销时间" disabled>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination";
import { getAllRetailer } from "@/api/member";
import {
  getBalanceBills,
  delBalanceBill,
  delBalanceBills,
} from "@/api/card";

export default {
  name: "BalanceBill",
  data() {
    return {
      title: "商家核销记录",
      form: {},
      total: 0,
      datas: [],
      retailers: [],
      selections: [],
      listLoading: true,

      dialogType: "new",
      dialogVisible: false,

      listQuery: {
        page: 1,
        page_size: 20,
        retailer_id: null,
      },

      tableColumns: [
        {
          key: "retailer_name",
          title: "商家名称",
          align: "left",
        },

        {
          key: "bill_date",
          title: "结算时间",
          align: "center",

        },
        {
          key: "counts",
          title: "结算数量",
          align: "center",
        },
        {
          key: "name",
          title: "结算人",
          align: "center",
        }
      ],
    };
  },
  components: {
    Pagination,
  },
  computed: {
    getTitle() {
      return this.title
    },
  },
  mounted() {
    this.getBalanceBills();
    this.getRetailers();
  },
  methods: {
    getBalanceBills() {
      this.listLoading = true;
      getBalanceBills(this.listQuery)
        .then((res) => {
          this.datas = res.results;
          this.total = res.count;
          this.listLoading = false;
        })
        .catch((err) => {
          this.listLoading = false;
          this.$message.error("获取数据失败!");
        });
    },

    // 修改
    handleEdit(scope) {
      this.form = Object.assign({}, scope.row);

      this.dialogType = "edit";
      this.dialogVisible = true;

      this.$nextTick(() => {
        this.$refs.formRef.clearValidate();
      });
    },

    //获取商家
    getRetailers() {
      getAllRetailer()
        .then((res) => {
          this.retailers = res;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    
    handleFilter() {
      this.listQuery.page = 1;
      this.getBalanceBills();
    },

    selectChange(value) {
      // const card = this.cards.find((v) => v.id == value);
      // this.form.comment = card.comment;
    },


    //删除数据
    handleDelete({ $index, row }) {
      this.$confirm("确定要删除吗?", "删除提示：", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await delBalanceBill(row.id);

          this.getBalanceBills();

          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch((err) => {
          console.error(err);
        });
    },

    handleBatchDelete() {
      if (this.selections.length == 0) {
        this.$message.warning("请选择要删除记录");
        return;
      }
      this.$confirm("确定要删除选中记录吗?", "删除提示：", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const sel_ids = {
            select_ids: this.selections,
          };
          const res = await delBalanceBills(sel_ids);

          this.getBalanceBills();

          this.$message({
            type: "success",
            message: "删除成功!",
          });

        })
        .catch((err) => {
          console.error(err);
        });
    },

    handleSelectionChange(val) {
      this.selections = val.map((item) => item.id);
    },
  },
};
</script>

<style lang="scss" scoped>
.item {
  margin-top: 8px;
}
</style>